package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public Map<String, Object> getItem(Long albumId) {
        //创建集合对象
        HashMap<String, Object> result = new HashMap<>();
        //利用多线程查询数据并回显
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //远程调用
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            //判断
            Assert.notNull(albumInfoResult, "专辑结果集不存在");
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "专辑对象不存在");
            result.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolExecutor);
        //获取分类数据
        CompletableFuture<Void> cateCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<BaseCategoryView> CategoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(CategoryViewResult, "分类数据不存在");
            BaseCategoryView baseCategoryView = CategoryViewResult.getData();
            Assert.notNull(baseCategoryView, "分类对象不存在");
            //存储数据
            result.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor);
        //获取用户信息
        CompletableFuture<Void> userInfoCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {

            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
            Assert.notNull(userInfoVoResult, "用户信息不存在");
            UserInfoVo userInfo = userInfoVoResult.getData();
            Assert.notNull(userInfo, "用户对象不存在");
            //存储数据
            result.put("announcer", userInfo);
        }, threadPoolExecutor);
        //  获取专辑统计信息.
        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {
            //  远程调用
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStatistics(albumId);
            Assert.notNull(albumStatVoResult, "专辑统计信息不存在");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "专辑统计信息不存在");
            result.put("albumStatVo", albumStatVo);
        },threadPoolExecutor);

        //  多任务组合
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                cateCompletableFuture,
                userInfoCompletableFuture,
                statCompletableFuture
        ).join();


        //  返回数据
        return result;
    }
}
